package com.cuit.riskassessment.action.result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cuit.riskassessment.service.ProjectService;
import com.cuit.riskassessment.service.RaService;
import com.cuit.riskassessment.service.RbService;
import com.cuit.riskassessment.service.RcService;
import com.cuit.riskassessment.service.RmService;
import com.cuit.riskassessment.service.RuService;
import com.cuit.riskassessment.service.RvService;
import com.cuit.riskassessment.util.Chart;
import com.cuit.riskassessment.util.INFO;
import com.cuit.riskassessment.util.Result;
import com.cuit.riskassessment.util.Tree;
import com.cuit.riskassessment.vo.RCParam;
import com.cuit.riskassessment.vo.RaParam;
import com.cuit.riskassessment.vo.Rb;
import com.cuit.riskassessment.vo.RmParam;
import com.cuit.riskassessment.vo.RuParam;
import com.cuit.riskassessment.vo.Rv;

/**
 * 获取最终的结果信息
 * @author Yang_Zhiqin-pc
 *
 */
@Controller
@RequestMapping(value="res")
public class ResultAction {

	@Autowired
	@Resource(name="raService")
	private RaService raService;

	@Autowired
	@Resource(name="rbService")
	private RbService rbService;

	@Autowired
	@Resource(name="rcService")
	private RcService rcService;

	@Autowired
	@Resource(name="rmService")
	private RmService rmService;

	@Autowired
	@Resource(name="ruService")
	private RuService ruService;

	@Autowired
	@Resource(name="rvService")
	private RvService rvService;
	
	/*@Autowired
	@Resource(name="areaService")
	private AreaService areaService;*/
	
	@Autowired
	@Resource(name="projectService")
	private ProjectService projectService;
	
	@RequestMapping(value="/data")
	public @ResponseBody Object getResult1(HttpServletResponse res, @RequestParam(value="projid") Integer projid)
	{
		res.setHeader("Accept",  "application/json");
		Result result = new Result();
		
		try{
			
			RaParam RA = this.raService.getRaVo(projid);
			Rb      RB = this.rbService.setRb(projid);
			RCParam RC = this.rcService.getRc(projid);
			RmParam RM = this.rmService.getRm(projid);
			Rv      RV = this.rvService.setRv(projid);
			//电力
			RuParam RU1 = this.ruService.getRu1(projid);
			// 通信
			RuParam RU2 = this.ruService.getRu2(projid);
			
			double r1 = 0;
			double r2 = 0;
			double r3 = 0;
			
			Tree tree = this.projectService.getProject(projid);
			
			//计算R1 分两种情况
			double ra = valiData(RA.getRaResult());
			double r1rb =valiData( RB.getR1rb());
			double r2rb =valiData(RB.getR2rb()); 
			double r3rb =valiData(RB.getR3rb()); 
			double r1rc =valiData(RC.getRcResult1());
			double r2rc =valiData(RC.getRcResult1());
			double r1rm   = valiData(RM.getR1RmResult());
			double r2rm   = valiData(RM.getR2RmResult());
			
			double rup    = valiData(RU1.getRuResult());
			double ruc    = valiData(RU2.getRuResult());
			//电力
			double r1rvp  = valiData(RV.getR1rvpow());
			//通信
			double r1rvc  = valiData(RV.getR1rvcom());;
			//电力
			double r2rvp  = valiData(RV.getR2rvpow());;
			//通信
			double r2rvc  = valiData(RV.getR2rvcom());
			//电力
			double r3rvp  = valiData(RV.getR3rvpow());;
			//通信
			double r3rvc  = valiData(RV.getR3rvcom());
			double r1rwp = valiData(RV.getR1rwpow());
			double r1rwc = valiData(RV.getR1rwcom());
			double r2rwp = valiData(RV.getR2rwpow());
			double r2rwc = valiData(RV.getR2rwcom());
			double r1zp = valiData(RV.getR1rzpow());
			double r1zc = valiData(RV.getR1rzcom());
			double r2zp = valiData(RV.getR2rzpow());
			double r2zc = valiData(RV.getR2rzcom());
			
			if(tree.getType()==3)
			{
				//一般建筑物 r1 = Ra+Rb+Ru+Rv;
				r1 = ra +r1rb+(rup+ruc)+(r1rvp+r1rvc);
			}else
			{
				//其他
					r1 = ra +r1rb+(rup+ruc)+(r1rvp+r1rvc)+r1rc+r1rm+(r1rwp+r1rwc)+(r1zp+r1zc);
			}
			r2 = r2rb + r2rc + r2rm +(r2rvp+r2rvc)+(r2rwp+r2rwc)+(r2zp+r2zc);
			r3 = r3rb + (r3rvp+r3rvc);
			Double a  = ((ra+r1rb)/r1)*100;
			Double bp = ((rup+r1rvp+r1rwp+r1zp)/r1)*100;
			Double bc = ((ruc+r1rvc+r1rwc+r1zc)/r1)*100;
			Double c  = 0.0;
			//tree.getType()!=1&&tree.getType()!=0
			if(tree.getType()!=3&&tree.getType()!=0)
			{
				c = (r1rc+(r1rm+r2rm)/r1)*100;
			}else
			{
				c = ((valiData(RC.getRcWithRC())+r1rm+r2rm)/r1)*100;
			}
			
			
			Map<String ,Object> map = new HashMap<String,Object>();
			
			//表单中的信息
			Map<String ,Object> form = new HashMap<String,Object>();
			
			form.put("projname", tree.getText());
			form.put("projtype", INFO.types.get(tree.getType()));
			form.put("r1", r1);
			form.put("r2", r2);
			form.put("r3", r3);
		  //表单中的信息
			map.put("form", form);
			
			 //chart中的数据
			 List<Object> chart = new ArrayList<Object>();
			 chart.add(new Chart("R1",r1));
			 chart.add(new Chart("R2",r2));
			 chart.add(new Chart("R3",r3));
			 map.put("charts",chart);
			 map.put("pies1",new Chart[]{new Chart("雷击建筑物风险损失所占比例为",a)});
			 map.put("pies2",new Chart[]{new Chart("雷击建筑物风险损失所占比例为",bp)});
			 map.put("pies3",new Chart[]{new Chart("雷击建筑物风险损失所占比例为",bc)});
			 map.put("pies4",new Chart[]{new Chart("雷击建筑物风险损失所占比例为",c)});
			 
			result.setOthers(map);
			result.setSuccess(true);
			}catch(Exception e)
			{
				result.setSuccess(false);
			}
			
		
		    
		
	//		map.put("r1", r1);
	//		map.put("r2", r2);
	//		map.put("r3", r3);
	//		map.put("a", a);
	//		map.put("bp", bp);
	//		map.put("bc", bc);
	//		map.put("c", c);
		return result;
	}
	/**
	 * 数据校验 将null 转化成0
	 * @param d
	 * @return
	 */
	public double valiData(Double d)
	{
		return d==null?0:d;
	}
}
